<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '查看'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="品牌名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="商品名称" style="width:500px"></el-input>
      </el-form-item>
      <el-form-item label="品牌编号" prop="code">
        <el-input v-model="dataForm.code" placeholder="品牌编号" style="width:500px"></el-input>
      </el-form-item>
      <el-form-item label="品牌英文名" prop="englishName">
        <el-input v-model.trim="dataForm.englishName" placeholder="品牌英文名" style="width:500px"></el-input>
      </el-form-item>
      <el-form-item label="品牌缩写" prop="nameAbbreviation">
        <el-input v-model="dataForm.nameAbbreviation" placeholder="品牌缩写" style="width:500px"></el-input>
      </el-form-item>
      <el-form-item label="品牌产地" prop="placeOrigin">
        <el-input v-model="dataForm.placeOrigin" placeholder="品牌产地" style="width:500px"></el-input>
      </el-form-item>
      <el-form-item label="官方网址" prop="officialWebsite">
        <el-input v-model="dataForm.officialWebsite" placeholder="官方网址" style="width:500px"></el-input>
      </el-form-item>
      <el-form-item label="品牌排序" prop="sort">
        <el-input v-model="dataForm.sort" placeholder="品牌排序" style="width:500px"></el-input>
      </el-form-item>
      <el-table v-if="this.dataForm.classList.length != 0"
                         :data="this.dataForm.classList"
                         border
                         style="width: 100%">
        <el-table-column v-show="flag == true"
          prop="selectSubClassId"
          header-align="center"
          align="center"
          label="二级分类ID">
        </el-table-column>
        <el-table-column
          prop="selectSubClassName"
          header-align="center"
          align="center"
          label="二级分类名称">
        </el-table-column>
      </el-table>
      <el-form-item label="是否启用" prop="enableState">
        <template>
          <el-radio v-model="dataForm.enableState" :label="0">否</el-radio>
          <el-radio v-model="dataForm.enableState" :label="1">是</el-radio>
        </template>
      </el-form-item>
      <el-form-item label="品牌logo图" prop="logoUrl">
        <el-upload
          class="avatar-uploader"
          :action="this.$http.adornUrl(`/brand/uploadBrand?token=${this.$cookie.get('token')}`)"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload">
          <img v-if="dataForm.logoUrl" :src="dataForm.logoUrl" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="品牌大图" prop="imageUrl">
        <el-upload
          class="avatar-uploader"
          :action="this.$http.adornUrl(`/brand/uploadBrand?token=${this.$cookie.get('token')}`)"
          :show-file-list="false"
          :on-success="handleAvatarSuccess1"
          :before-upload="beforeAvatarUpload">
          <img v-if="dataForm.imageUrl" :src="dataForm.imageUrl" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="品牌故事" prop="story">
        <el-input type="textarea" v-model="dataForm.story" placeholder="品牌故事"></el-input>
      </el-form-item>
      <el-form-item label="描述" prop="remarks">
        <el-input v-model="dataForm.remarks"  type="textarea" placeholder="描述"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
    </span>
  </el-dialog>
</template>
<script>
  export default {
    data () {
      return {
        flag: false,
        visible: false,
        dialogImageUrl: '',
        dialogVisible: false,
        addOrUpdateVisible: false,
        dataForm: {
          id: 0,
          code: '',
          name: '',
          englishName: '',
          placeOrigin: '',
          nameAbbreviation: '',
          officialWebsite: '',
          sort: '',
          enableState: '',
          createTime: '',
          story: '',
          remarks: '',
          logoUrl: '',
          classList: [],
          imageUrl: ''
        }
      }
    },
    methods: {
      init (id) {
        debugger
        this.dataForm.logoUrl = ''
        this.dataForm.imageUrl = ''
        this.dataForm.classList = []
        this.selectParentClass = -1
        this.selectSubClass = ''
        this.visible = true
        this.dataForm.id = id || 0
        if (this.$refs['dataForm'] !== undefined) {
          this.$refs['dataForm'].resetFields()
        }
        debugger
        this.$nextTick(() => {
          debugger
          if (this.$refs['dataForm'] !== undefined) {
            this.$refs['dataForm'].resetFields()
          }
          this.$http({
            url: this.$http.adornUrl(`/activity/goods/queryClassList`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.goodsClassList = data.data
            }
            console.log(this.goodsClassList)
            console.log(this.dataForm.classList)
          })
          if (this.dataForm.id) {
            debugger
            this.$http({
              url: this.$http.adornUrl(`/brand/getBrandById/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                debugger
                this.dataForm.name = data.data.name
                this.dataForm.code = data.data.code
                this.dataForm.nameAbbreviation = data.data.nameAbbreviation
                this.dataForm.officialWebsite = data.data.officialWebsite
                this.dataForm.sort = data.data.sort
                this.dataForm.enableState = data.data.enableState
                this.dataForm.logoUrl = data.data.logoUrl != null ? 'https://image.yilianbaihui.cn/consumer/' + data.data.logoUrl : ''
                this.dataForm.imageUrl = data.data.imageUrl != null ? 'https://image.yilianbaihui.cn/consumer/' + data.data.imageUrl : ''
                this.dataForm.story = data.data.story
                this.dataForm.remarks = data.data.remarks
                this.dataForm.englishName = data.data.englishName
                this.dataForm.placeOrigin = data.data.placeOrigin
                for (var i = 0; i < data.data.list.length; i++) {
                  this.dataForm.classList.push({selectSubClassId: data.data.list[i].id, selectSubClassName: data.data.list[i].className})
                }
              }
            })
          }
        })
      }
    }
  }
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 128px;
    height: 128px;
    line-height: 128px;
    text-align: center;
  }
  .avatar {
    width: 128px;
    height: 128px;
    display: block;
  }
</style>
